package cn.com.elite.workorder;

import java.io.PrintWriter;

import mlink.esms.api.simpleclient.SimpleClientExample;

import org.apache.struts.action.*;

import cn.com.elite.util.Util;

import javax.servlet.http.*;
import javax.sql.DataSource;

public class sendSMS extends Action {
//	static cn.com.elite.workorder.WorkOrderBean workorderBean;
//	static DataSource dataSource = null;
//	static {
//		dataSource = (DataSource) ContextHelper.getContext().getBean(
//		"dataSource");
//		workorderBean.setDataSource(dataSource);
//	}
	cn.com.elite.workorder.WorkOrderBean workorderBean;
	public WorkOrderBean getWorkorderBean() {
		return workorderBean;
	}

	public void setWorkorderBean(WorkOrderBean workorderBean) {
		this.workorderBean = workorderBean;
	}
	  public ActionForward execute(ActionMapping actionMapping, 
			  ActionForm actionForm, HttpServletRequest req, 
			  HttpServletResponse resp) {
		  try {
			  
				System.out.println("---------------------------------------------");
				//http://116.236.129.6:8080/SMS/workorder/sendSMS.do?command=MO_REQUEST&spid=1&sppassword=abc&momsgid=118464091310100268&da=555501938&sa=8613647711379&dc=15&sm=c4e3bac332303038
				String spid = req.getParameter("spid");
		        String momsgid = req.getParameter("momsgid");
		        String da = req.getParameter("da");
		        String sa = req.getParameter("sa");
		        String dc = req.getParameter("dc");
		        String sm = req.getParameter("sm");
		        System.out.println(req.getQueryString());
		        //将Hex编码的上行内容还原成普通字符串
		        //在jdk1.4版本环境下，请使用DIYClientExample.decodeHexStr(Integer.parseInt(dc),sm)
		        String moContent = "";
		        try {
		        	moContent = SimpleClientExample.decodeHexStr(Integer.parseInt(dc),sm);     
		        } catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		        System.out.println(sa);
		        System.out.println(da);
		        System.out.println(moContent);
		        
		        //获取上行参数后，请即刻返回响应信息
		        PrintWriter pw;
				
					pw = resp.getWriter();
				
		        StringBuilder output = new StringBuilder();
		        output.append("command=MO_RESPONSE&spid=");
		        output.append(spid);
		        output.append("&momsgid=");
		        output.append(momsgid);
		        output.append("&mostat=ACCEPT&moerrcode=000");
		        resp.setContentLength(output.length());
		        pw.write(output.toString());
		        pw.flush();
		        pw.close();

		        //处理逻辑
		        System.out.println(sa);
		        System.out.println(da);
		        System.out.println(moContent);
		        /*
		         process code
		         ......
		         */
				String sql = "insert into documentin (Documentin_GUID, DocType,Status,  DocumentPath, DocumentFrom, DocumentTo, ModifiedBy, ModifiedDate, RECEIVEDDATETIME )"
		+	" values(Sys_Guid(), 3, 0,  '"+Util.filter(moContent)+"', '"+Util.filter(sa)+"', '"+Util.filter(da)+"', 'elite', sysdate, sysdate) ";
				int ret = workorderBean.insertinto(sql);
//				System.out.println("更新数量="+ret);
		        } catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return null;
	  }
}
